Conditions | 35 |
Total Lines | 57 |
Code Lines | 42 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like datatables.js ➔ deparam often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | /** |
||
127 | function deparam(params, coerce) { |
||
128 | var obj = {}, |
||
129 | coerce_types = {'true': !0, 'false': !1, 'null': null}; |
||
130 | $.each(params.replace(/\+/g, ' ').split('&'), function (j, v) { |
||
131 | var param = v.split('='), |
||
132 | key = decodeURIComponent(param[0]), |
||
133 | val, |
||
134 | cur = obj, |
||
135 | i = 0, |
||
136 | keys = key.split(']['), |
||
137 | keys_last = keys.length - 1; |
||
138 | |||
139 | if (/\[/.test(keys[0]) && /\]$/.test(keys[keys_last])) { |
||
140 | keys[keys_last] = keys[keys_last].replace(/\]$/, ''); |
||
141 | keys = keys.shift().split('[').concat(keys); |
||
142 | keys_last = keys.length - 1; |
||
143 | } else { |
||
144 | keys_last = 0; |
||
145 | } |
||
146 | |||
147 | if (param.length === 2) { |
||
148 | val = decodeURIComponent(param[1]); |
||
149 | |||
150 | if (coerce) { |
||
151 | val = val && !isNaN(val) ? +val // number |
||
152 | : val === 'undefined' ? undefined // undefined |
||
153 | : coerce_types[val] !== undefined ? coerce_types[val] // true, false, null |
||
154 | : val; // string |
||
155 | } |
||
156 | |||
157 | if (keys_last) { |
||
158 | for (; i <= keys_last; i++) { |
||
159 | key = keys[i] === '' ? cur.length : keys[i]; |
||
160 | cur = cur[key] = i < keys_last |
||
161 | ? cur[key] || (keys[i + 1] && isNaN(keys[i + 1]) ? {} : []) |
||
162 | : val; |
||
163 | } |
||
164 | |||
165 | } else { |
||
166 | if ($.isArray(obj[key])) { |
||
167 | obj[key].push(val); |
||
168 | } else if (obj[key] !== undefined) { |
||
169 | obj[key] = [obj[key], val]; |
||
170 | } else { |
||
171 | obj[key] = val; |
||
172 | } |
||
173 | } |
||
174 | |||
175 | } else if (key) { |
||
176 | obj[key] = coerce |
||
177 | ? undefined |
||
178 | : ''; |
||
179 | } |
||
180 | }); |
||
181 | |||
182 | return obj; |
||
183 | } |
||
184 | }($)); |
||
185 |